define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
	
	$scope.form.fk_client =params.fk_client;
	$scope.form.pk_app =params.fk_client;
	$scope.form.fk_roleid = params.fk_roleid;

	$scope.find=function(){		
		httpService.post(app.urls.menu_select,$scope.form,function(data){
			if("0000" === data.code){
				$scope.menus = data.data;
				$scope.selectAllButton();
			}else{
				alert(data.msg);
			}
		});
	}
	$scope.find();
	$scope.selectAllButton = function(){
		httpService.post(app.urls.button_selectByApp,$scope.form,function(data){
			if("0000" === data.code){
				$scope.buttons = data.data;
				$scope.selectMenu();
			}else{
				alert(data.msg);
			}
		});
	}
	
	$scope.selectMenu = function(){
		httpService.post(app.urls.menu_selectRoleMenu,$scope.form,function(data){
			if("0000" === data.code){
				if(data.data.length > 0){
					$scope.menulist = data.data;
					for(var i =0;i<$scope.menulist.length;i++){
						$("#"+"menu_"+$scope.menulist[i].id).prop("checked", true);
					}
					$scope.selectButton();
				}
			}else{
				alert(data.msg);
			}
		});
	}
	$scope.selectButton = function(){
		httpService.post(app.urls.button_selectRoleButton,$scope.form,function(data){
			if("0000" === data.code){
				$scope.buttonlist = data.data;
				for(var i =0;i<$scope.buttonlist.length;i++){
					$("#"+"button_"+$scope.buttonlist[i].id).prop("checked", true);
				}
    		}else{
    			alert(data.msg);
    		}
		});
	}
	

	
	
	//返回应用
	$scope.goback=function(){
		var menu = {"url":"modules/content/usermanagement/userRole/list?FatherControllerName="+$scope.controllerName+"&type="+params.type+"&fk_roleid="+params.fk_roleid+"&fk_groupid="+params.fk_groupid};
		eventBusService.publish("modules.content.load",menu)
	}
				
	
	
//	//点击按钮事件
//	$scope.checkbutton = function(button){
//		$scope.form.button_id = button.id;
//		if($("input[id=button_"+button.id+"]").is(':checked')){		
//			httpService.post(app.urls.menu_selectByButton,$scope.form,function(data){
//				if("0000" === data.code){
//					$scope.menulist = data.data;
//					for(var i =0;i<$scope.menulist.length;i++){
//						$("#"+"menu_"+$scope.menulist[i].id).prop("checked", true);
//					}
//	    		}else{
//	    			alert(data.msg);
//	    		}
//			});
//	}
//		
//	}
	
	$scope.checkmenu = function(menu){
		$scope.form.id = menu.id;
		$scope.form.fk_menuid = menu.id;
		
		if($("input[id=menu_"+menu.id+"]").is(':checked')){		
			httpService.post(app.urls.menu_selectByMenu,$scope.form,function(data){
				if("0000" === data.code){
					$scope.menulist = data.data;
					for(var i =0;i<$scope.menulist.length;i++){
						$("#"+"menu_"+$scope.menulist[i].id).prop("checked", true);
						if(i==$scope.menulist.length-1){
							$scope.selectButtonBymenu();
						}
					}
	    		}else{
	    			alert(data.msg);
	    		}
			});
		}else{
			if(menu.father_id == 'ROOT'){
				httpService.post(app.urls.menu_selectByMenu,$scope.form,function(data){
					if("0000" === data.code){
						$scope.menulist = data.data;
						for(var i =0;i<$scope.menulist.length;i++){
							$("#"+"menu_"+$scope.menulist[i].id).prop("checked", false);
							if(i==$scope.menulist.length-1){
								$scope.selectButtonBymenus();
							}
						}
		    		}else{
		    			alert(data.msg);
		    		}
				});
			}else {
				httpService.post(app.urls.menu_selectByMenu,$scope.form,function(data){
					if("0000" === data.code){
						$scope.menulist = data.data;
						for(var i =0;i<$scope.menulist.length;i++){
							if($scope.menulist[i].id == menu.father_id){
								
							}else if($scope.menulist[i].father_id == 'ROOT'){
								
							}else{
								$("#"+"menu_"+$scope.menulist[i].id).prop("checked", false);
							}
							if(i==$scope.menulist.length-1){
								$scope.selectButtonBymenus();
							}
						}
		    		}else{
		    			alert(data.msg);
		    		}
				});
				
				
				
			}
		}
		
	}
	//菜单选中按钮
	$scope.selectButtonBymenu = function(){
		httpService.post(app.urls.button_selectByMenu,$scope.form,function(data){
			if("0000" === data.code){
				$scope.buttonlist = data.data;
				for(var i =0;i<$scope.buttonlist.length;i++){
					$("#"+"button_"+$scope.buttonlist[i].id).prop("checked", true);
				}
    		}else{
    			alert(data.msg);
    		}
		});
	}
	
	//菜单取消按钮
	$scope.selectButtonBymenus = function(){
		httpService.post(app.urls.button_selectByMenu,$scope.form,function(data){
			if("0000" === data.code){
				$scope.buttonlist = data.data;
				for(var i =0;i<$scope.buttonlist.length;i++){
					$("#"+"button_"+$scope.buttonlist[i].id).prop("checked", false);
				}
    		}else{
    			alert(data.msg);
    		}
		});
	}
	
	$scope.add = function(){
		$scope.delete();
	}
	
	$scope.delete = function(){
		$scope.form.type = '3';
		httpService.post(app.urls.role_power_delete,$scope.form,function(data){
			if("0000" === data.code){
				$scope.deleteMenu();				
    		}else{
    			alert(data.msg);
    		}
		});
	}
	
	$scope.deleteMenu = function(){
		$scope.form.type = '2';
		httpService.post(app.urls.role_power_delete,$scope.form,function(data){
			if("0000" === data.code){
				$scope.addMenuAndButton();
    		}else{
    			alert(data.msg);
    		}
		});
	}
	
	$scope.addMenuAndButton = function(){
		var menuvalues = [];
		var buttonvalues = [];
		var buttonmenus = [];
    	
		$('#'+$scope.controllerName+' input[name="dataPk_menu"]:checked').each(function(){ 
			menuvalues.push($(this).val());
    	});
		
		$('#'+$scope.controllerName+' input[name="dataPk_button"]:checked').each(function(){ 
			buttonvalues.push($(this).val().split(',')[0]);
			buttonmenus.push($(this).val().split(',')[1]);
    	});
		
    	if(menuvalues.length < 1 && buttonvalues.length < 1){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择添加数据。"});
    	}else{
    		$scope.form.menuvalues = JSON.stringify(menuvalues);
    		$scope.form.buttonvalues = JSON.stringify(buttonvalues);
    		$scope.form.buttonmenus = JSON.stringify(buttonmenus);

    		httpService.post(app.urls.role_power_all_insert,$scope.form,function(data){
				if(1 === data){
    				alert("保存成功");
        		}else{
        			alert("保存失败");
        		}
    		});
    		
    	}
	}
	
	
}}});